import os
import inspect
import fnmatch

# try to import an environment first

Import('env')
env = env.Clone()

try:
    Import('cuda_support')
except:
    cuda_support = False

abscur = os.path.abspath(os.curdir)
parent, current = os.path.split(os.path.abspath(os.curdir))
backend_dir = os.path.join(parent, 'backend')
backend_inc_dir = os.path.join(backend_dir, 'inc')
env.Append(CPPPATH = [backend_inc_dir])

import distutils.sysconfig

try:
    Import('siteconf')
except:
    siteconf = {}

#Add Python includes
env.Append(CPPPATH = [distutils.sysconfig.get_python_inc()])
env.Append(LIBS = [siteconf.get('BOOST_PYTHON_LIBNAME')])
bid = siteconf.get('BOOST_INC_DIR', None)
bld = siteconf.get('BOOST_LIB_DIR', None)
np_inc_path = siteconf.get('NP_INC_PATH', None)
if bid:
    env.Append(CPPPATH = [bid])
if bld:
    env.Append(LIBPATH = [bld])

if env['PLATFORM'] == 'darwin':
    env.Append(LINKFLAGS=['-F/System/Library/Frameworks/', '-framework', 'Python'])


cppenv = env.Clone()

cudaenv = env.Clone()


cppenv.Append(CCFLAGS = ["-std=c++0x", "-Wall", "-O3"])

cudaenv.Append(CPPPATH = [np_inc_path])


if cuda_support:
    cudaenv.Append(LIBS = ['cudart'])
    cudaenv.Append(CCFLAGS = ['-DCUDA_SUPPORT'])
cudaenv.Append(CPPPATH = [os.path.join(backend_dir, os.path.join('prelude'))])


cudaenv_host = cudaenv.Clone()
cudaenv_host.Append(CCFLAGS = ["-std=c++0x", "-Wall", "-O3"])


extensions = []

#Build backend compiler
python_wrap_object = cppenv.SharedObject('copperhead/compiler/python_wrap.cpp')
sequence_extract_object = cppenv.SharedObject('copperhead/compiler/sequence_extract.cpp')
cuarray_extract_object = cppenv.SharedObject('copperhead/compiler/cuarray_extract.cpp')
namespace_wrap_object = cppenv.SharedObject('copperhead/compiler/namespace_wrap.cpp')
backend_compiler = cppenv.SharedLibrary(source=['copperhead/compiler/backendcompiler.cpp',
                                                python_wrap_object, namespace_wrap_object,
                                                sequence_extract_object, cuarray_extract_object],
    SHLIBPREFIX='',SHLIBSUFFIX='.so')
extensions.append(('copperhead/compiler/backendcompiler.cpp', backend_compiler))

cppenv_aug = cppenv.Clone()
cppenv_aug.Append(CPPPATH=[np_inc_path])

cunp_library = cppenv_aug.SharedLibrary('copperhead/runtime/cunp.cpp')
extensions.append(('copperhead/runtime/cunp.cpp', cunp_library))

#Build backendsyntax, backendtypes
for x in ['copperhead/compiler/backendsyntax.cpp',
          'copperhead/compiler/backendtypes.cpp',
          'copperhead/runtime/load.cpp']:
    ext = cppenv.SharedLibrary(source=x,
                               SHLIBPREFIX='',
                               SHLIBSUFFIX='.so')
    extensions.append((x, ext))


#Build cudata
for x in ['copperhead/runtime/cudata.cpp']:
    ext = cudaenv_host.SharedLibrary(source=[x],
                                     SHLIBPREFIX='',
                                     SHLIBSUFFIX='.so')
    extensions.append((x, ext))
    
#Build cuda_info
if cuda_support:
    for x in ['copperhead/runtime/cuda_info.cpp']:
        ext = cudaenv_host.SharedLibrary(source=[x],
                                         SHLIBPREFIX='',
                                         SHLIBSUFFIX='.so')
        extensions.append((x, ext))

Return('extensions')
